<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Header</title>
    <!-- 将CSS代码放到style标签中 -->
    <style>
        /* 设置背景颜色和文字颜色 */
        body {
            background-color: #f7f7f7;
            color: #333;
        }

        /* 将header部分设置为固定定位，覆盖在页面顶部 */
        #header {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1;
            background-color: #333;
            color: #fff;
            height: 50px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
        }

        /* 根据需要设置登录、注册、欢迎信息和注销链接的样式 */
        #header a {
            color: #fff;
            text-decoration: none;
            margin-right: 10px;
        }

        #header span {
            color: #ccc;
            margin-right: 10px;
        }

        /* 导航栏样式 */
        #nav {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        #nav a {
            display: block;
            color: #fff;
            text-decoration: none;
            margin: 0 10px;
            padding: 10px 20px;
            background-color: #007bff;
            text-align: center;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }

        #nav a:hover {
            background-color: #444;
        }
    </style>
    <script src="../js/axios.min.js"></script>
</head>
<body>
<header id="header">
    <!-- header.jsp页面的内容 -->
    <div>
        <h2>My Website</h2>

    </div>
    <nav id="nav">
        <c:choose>
            <c:when test="${empty sessionScope.user}">
                <a href="../user/login.jsp">Login</a>
                <a href="../user/register.jsp">Register</a>
            </c:when>
            <c:otherwise>
                <span><a href="../user/user.jsp">Welcome, ${sessionScope.user.username}!</a></span>
                <a href="" id="logout">Logout</a>
            </c:otherwise>
        </c:choose>

        <%--        <c:choose>--%>
        <%--            <c:when test="${empty cookie.user.value}">--%>
        <%--                <a href="../user/login.jsp">Login</a>--%>
        <%--                <a href="../user/register.jsp">Register</a>--%>
        <%--            </c:when>--%>
        <%--            <c:otherwise>--%>
        <%--                <span><a href="../user/user.jsp">Welcome, ${cookie.user.value}!</a></span>--%>
        <%--                <a href="" id="logout">Logout</a>--%>
        <%--            </c:otherwise>--%>
        <%--        </c:choose>--%>
    </nav>
</header>
</body>
<script>
    const logoutLink = document.getElementById('logout');
    logoutLink.addEventListener('click', (event) => {
        event.preventDefault(); // 阻止默认的链接跳转行为
        axios.get('/logout') // 发送一个POST请求到/logout路由
            .then((response) => {
                // 成功响应后的处理逻辑
                window.location.href = '/user/login.jsp'; // 重定向到登录页面
            })
            .catch((error) => {
                // 发生错误时的处理逻辑
                console.error(error);
            });
    });
</script>
</html>
